
package com.ruoyi.client.controller;

import com.github.pagehelper.PageInfo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.client.pojo.form.OrderInsertForm;
import com.ruoyi.client.pojo.form.OrderQueryPageForm;
import com.ruoyi.client.pojo.form.OrderUpdateStatusForm;
import com.ruoyi.client.pojo.vo.OrderDetailVo;
import com.ruoyi.client.pojo.vo.PageListVo;
import com.ruoyi.client.service.OrderService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Anonymous
@CrossOrigin
@RestController("ClientOrderController")
@Api(value = "v1", tags = "07.订单模块")
@RequestMapping("order")
public class OrderController extends BaseController {

    @Resource
    private OrderService orderService;

    /**
     * 增加订单（从商品列表创建）
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "增加订单（从商品列表创建建）", notes = "从商品列表创建新的订单，接口参数：地址id、商品列表")
    @ApiImplicitParam(name = "form", value = "增加订单参数", dataType = "OrderInsertForm", required = true)
    @PostMapping("/insert")
    public R<Long> insert(@Valid @RequestBody OrderInsertForm form) {
        //保存订单并返回订单号
        Long orderId = orderService.insert(form);
        return orderId > 0 ? R.ok(orderId) : R.fail();
    }

    /**
     * 更改订单状态(0初始  1取消 2完成)
     */
    @ApiOperationSupport(order = 20)
    @ApiOperation(value = "更改订单状态", notes = "更改订单状态(0初始 1取消 2完成)")
    @ApiImplicitParam(name = "form", value = "订单状态参数", dataType = "OrderUpdateStatusForm", required = true)
    @PostMapping("/updateStatus")
    public R updateStatus(@Valid @RequestBody OrderUpdateStatusForm form) {
        return orderService.updateStatus(form.getId(), form.getStatus()) ? R.ok() : R.fail();
    }

    /**
     * 查询订单详情By订单Id
     */
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "查询订单详情", notes = "返回订单id对应的订单详情")
    @ApiImplicitParam(name = "orderId", value = "订单id", required = true)
    @GetMapping("/{orderId}")
    public R<OrderDetailVo> selectDetailById(@PathVariable("orderId") Long orderId) {
        OrderDetailVo orderDetailVo = orderService.selectDetailById(orderId);
        return R.ok(orderDetailVo);
    }

    /**
     * 查询我的订单列表(分页)
     */
    @ApiOperationSupport(order = 31)
    @ApiOperation(value = "查询我的订单列表", notes = "返回我的订单列表")
    @GetMapping("/getMyList")  //Ruoyi框架分页查询，使用GET。为什么？自行分析框架源码
    public R<PageListVo<OrderDetailVo>> list(@Valid OrderQueryPageForm form)
    {
        startPage();//设置分页，调用的父方法
        List<OrderDetailVo> list = orderService.selectMyList(form); // 获取订单数据（不含商品项）

        PageInfo pageInfo = new PageInfo(list);
        PageListVo<OrderDetailVo> pageListVo = new PageListVo<>();

        pageListVo.setTotalCount(pageInfo.getTotal());
        pageListVo.setTotalPage(pageInfo.getPages());
        pageListVo.setList(list);

        return R.ok(pageListVo);  // 接口返回
    }
}
